package com.csaba.connector.fhb;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.csaba.connector.DummyServer;
import com.csaba.connector.ServiceException;
import com.csaba.connector.fhb.model.FHBRequestFactory;
import com.csaba.connector.fhb.model.FHBSession;
import com.csaba.connector.model.Session;
import com.csaba.connector.service.AbstractBankService;
import com.csaba.connector.service.LogoutService;

public class FHBLogoutService extends AbstractBankService implements LogoutService
{
	@Override
	public void execute( final Session session ) throws ServiceException
	{
		if ( !( session instanceof FHBSession ) )
			throw new ServiceException("Unsupported session for OTP services: " + session.getClass().getName());
		DummyServer.getInstance().removeSession(session);

		final FHBSession fhb = (FHBSession) session;
		//logout process
		//		type=logout
		//		csrfid=
		//		time=1317581644964
		//		CSRFValidationCode=ee1f10482c89b8c6b08302930643982
		//		ajaxrequest=true
		//		:_CLIENT_TRANSFORMATION__=true

		final NameValuePair[] logoutData = fhb.getParamTemplate(1);
		logoutData[0] = new BasicNameValuePair("type", "logout");

		final String logoutResult = fhb.doPost(
				FHBRequestFactory.getInstance().getAction(FHBRequestFactory.ACTION_LOGOUT_PROCESS, null), logoutData);
		fhb.debugFile("logoutResult", logoutResult);

		session.invalidateSession();
	}

}
